<template>
  <div class="multi-tab-content" :style="defaultStyle" :class="isActiveClass">
    <div class="tab-container" :style="contentStyle">
      <!-- 分类 tab -->
      <div class="tab-header">
        <div v-for="(tab, index) in contentData.tabs" :key="index" class="tab-item"
          :class="{ active: currentTabIndex === index }" :style="tabItemStyle(index)" @click="currentTabIndex = index">
          <div>{{ tab.category }}</div>
        </div>
      </div>

      <!-- 当前分类下的内容列表 -->
      <div class="tab-content" v-if="contentData.item_layout === 'list'">
        <div class="list-item" v-for="item in contentData.tabs[currentTabIndex]?.default_sub_list" :key="item.id">
          <div class="left-image">
            <img class="img" :src="item.image" alt="" />
          </div>
          <div class="right-text" v-if="contentData.tabs[currentTabIndex].type !== 'coach'">
            <div>
              <div class="title" v-if="contentData.show_item_name !== 0" :style="titleStyle">{{
                item.name
              }}</div>
              <div class="desc" v-if="contentData.show_item_desc !== 0" :style="descStyle">{{
                item.remark
              }}</div>
            </div>
            <p class="price" v-if="
              contentData.tabs[currentTabIndex].type === 'package' ||
              contentData.tabs[currentTabIndex].type === 'card'
            "><span class="unit">￥</span>{{ item.price }}</p>
          </div>

          <div class="right-text" v-if="contentData.tabs[currentTabIndex].type === 'coach'">
            <div>
              <div class="title mgB-10" v-if="contentData.show_item_name !== 0" :style="titleStyle">
                {{ item.name }}
              </div>
              <div class="flex-between mgB-10">
                <div class="flex align-center">
                  <div class="flex-center sex-man" v-if="item.gender === 0">
                    <img class="icon" src="@/assets/img/coach-man.png" />
                    <span>男</span>
                  </div>

                  <div class="flex-center sex-woman" v-if="item.gender === 1">
                    <img class="icon" src="@/assets/img/coach-woman.png" />
                    <span>女</span>
                  </div>
                </div>

                <div class="flex align-center coach-phone">
                  <i class="iconfont-sys">&#xe776;</i>
                  <span>电话咨询</span>
                </div>
              </div>
              <div>
                <span class="coach-tip">教龄：{{ item.drivingYears }}年</span>
              </div>
            </div>
          </div>
        </div>
      </div>

      <div class="layout-row-2-card" v-if="contentData.item_layout === 'row-2'">
        <div class="card-item" v-for="item in contentData.tabs[currentTabIndex].default_sub_list" :key="item.id">
          <div class="card-image">
            <img class="img" :src="item.image" alt="" />
          </div>
          <div class="card-text" v-if="contentData.tabs[currentTabIndex].type !== 'coach'">
            <div class="title" v-if="contentData.show_item_name !== 0" :style="titleStyle">{{
              item.name
            }}</div>
            <div class="desc" v-if="contentData.show_item_desc !== 0" :style="descStyle">{{
              item.remark
            }}</div>

            <div class="price" v-if="
              contentData.tabs[currentTabIndex].type === 'package' ||
              contentData.tabs[currentTabIndex].type === 'card'
            ">{{ item.price }}</div>
          </div>

          <div class="card-text" v-if="contentData.tabs[currentTabIndex].type === 'coach'">
            <div>
              <div class="title mgB-10" v-if="contentData.show_item_name !== 0" :style="titleStyle">
                {{ item.name }}
              </div>
              <div class="flex-between mgB-10">
                <div class="flex align-center">
                  <div class="flex-center sex-man" v-if="item.gender === 0">
                    <img class="icon" src="@/assets/img/coach-man.png" />
                    <span>男</span>
                  </div>

                  <div class="flex-center sex-woman" v-if="item.gender === 1">
                    <img class="icon" src="@/assets/img/coach-woman.png" />
                    <span>女</span>
                  </div>
                </div>

                <div class="flex align-center coach-phone">
                  <i class="iconfont-sys">&#xe776;</i>
                  <span>电话咨询</span>
                </div>
              </div>
              <div>
                <span class="coach-tip">教龄：{{ item.drivingYears }}年</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
const props = defineProps({
  contentData: {
    type: Object,
    default: () => ({})
  },
  isActiveClass: {
    type: String
  }
})

const currentTabIndex = ref(0)

const defaultStyle = computed(() => {
  const s = props.contentData.style
  return {
    paddingTop: s.paddingTop,
    paddingLeft: s.paddingLeft,
    paddingRight: s.paddingRight,
    paddingBottom: s.paddingBottom
  }
})

const contentStyle = computed(() => {
  const s = props.contentData.style
  return {
    borderRadius: s.borderRadius,
    backgroundColor: s.backgroundColor,
    overflow: 'hidden'
  }
})

function tabItemStyle(index: number) {
  const style = props.contentData.tabStyle || {}
  const isActive = currentTabIndex.value === index
  return {
    color: isActive ? style.titleColor || '#333' : style.inactiveTitleColor || '#999',
    fontSize: isActive ? `${style.tabFontSize}px` : `${style.tabFontSize - 4}px`
  }
}

// 标题样式
const titleStyle = computed(() => {
  const t = props.contentData.itemStyle?.title || {}
  let txtEllipsis = {}
  if (props.contentData.show_item_name == 1) {
    txtEllipsis = {
      overflow: 'hidden',
      whiteSpace: 'nowrap',
      textOverflow: 'ellipsis'
    }
    if (props.contentData.item_layout == 'list') {
      txtEllipsis = {
        width: '200px',
        overflow: 'hidden',
        whiteSpace: 'nowrap',
        textOverflow: 'ellipsis'
      }
    } else {
      txtEllipsis = {
        width: '160px',
        overflow: 'hidden',
        whiteSpace: 'nowrap',
        textOverflow: 'ellipsis'
      }
    }
  } else if (props.contentData.show_item_name == 2) {
    txtEllipsis = {
      display: '-webkit-box',
      overflow: 'hidden',
      WebkitBoxOrient: 'vertical',
      WebkitLineClamp: 2
    }
  }
  return {
    fontSize: t.fontSize ? `${t.fontSize}px` : '14px',
    color: t.color || '#333333',
    fontWeight: t.fontWeight === 0 ? 'normal' : t.fontWeight,
    ...txtEllipsis
  }
})

// 描述样式
const descStyle = computed(() => {
  const d = props.contentData.itemStyle?.desc || {}
  let txtEllipsis = {}
  if (props.contentData.show_item_desc == 1) {
    txtEllipsis = {
      overflow: 'hidden',
      whiteSpace: 'nowrap',
      textOverflow: 'ellipsis'
    }
    if (props.contentData.item_layout == 'list') {
      txtEllipsis = {
        width: '200px',
        overflow: 'hidden',
        whiteSpace: 'nowrap',
        textOverflow: 'ellipsis'
      }
    } else {
      txtEllipsis = {
        width: '160px',
        overflow: 'hidden',
        whiteSpace: 'nowrap',
        textOverflow: 'ellipsis'
      }
    }
  } else if (props.contentData.show_item_desc == 2) {
    txtEllipsis = {
      display: '-webkit-box',
      overflow: 'hidden',
      WebkitBoxOrient: 'vertical',
      WebkitLineClamp: 2
    }
  }
  return {
    color: d.color || '#999999',
    ...txtEllipsis
  }
})
</script>

<style scoped lang="scss">
.multi-tab-content {
  .tab-header {
    display: flex;
    gap: 16px;
    margin-bottom: 20px;

    .tab-item {
      position: relative;
      padding: 6px 12px;
      cursor: pointer;
    }

    .active {
      color: black;
      font-weight: 600;
    }

    .active::after {
      content: '';
      position: absolute;
      bottom: -5px;
      left: 50%;
      width: 0;
      height: 5px;
      background-color: v-bind('contentData.tabStyle?.underlineColor || "#03c36b"');
      border-radius: 12px;
      transform: translateX(-50%);
      transition: width 0.3s ease-out;
    }

    .active::after {
      width: 50%;
    }
  }

  .tab-content {
    display: flex;
    flex-direction: column;
    gap: 12px;

    .list-item {
      display: flex;
      background: #fff;
      // padding: 5px 10px 0 10px;
      padding: 5px 10px 0 0;
      //box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);

      .left-image {
        width: 120px;
        height: 80px;
        position: relative;

        img {
          width: 100%;
          height: 100%;
          object-fit: cover;
          border-radius: 6px;
        }

        .tag {
          position: absolute;
          background: rgba(0, 0, 0, 0.5);
          color: #fff;
          font-size: 10px;
          padding: 2px 4px;
          border-radius: 3px;
        }
      }

      .right-text {
        flex: 1;
        margin-left: 10px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;

        .title {
          font-weight: bold;
          color: #333;
          margin-bottom: 5px;
        }

        .desc {
          font-size: 12px;
          color: #999;
        }

        .price {
          font-size: 14px;
          color: #e74c3c;
          font-weight: 600;

          .unit {
            font-size: 12px;
          }
        }
      }
    }
  }

  .layout-row-2-card {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;

    .card-item {
      //width: calc(50% - 6px);
      width: calc((100% - 12px) / 2);
      background: #fff;
      box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
      overflow: hidden;
      display: flex;
      flex-direction: column;
    }

    .card-image {
      position: relative;
      width: 100%;
      height: 154px;
      //padding-top: 56%;
      background: #f5f5f5;

      .img {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        object-fit: cover;
      }

      .tag {
        position: absolute;
        font-size: 10px;
        padding: 2px 6px;
        border-radius: 3px;
        color: #fff;
      }
    }

    .card-text {
      padding: 8px;

      .title {
        font-weight: bold;
        font-size: 14px;
        color: #333;
        margin-bottom: 8px;
      }

      .desc {
        font-size: 12px;
        color: #999;
        margin-bottom: 8px;
      }

      .price {
        font-size: 14px;
        color: #e74c3c;
        font-weight: 600;
      }
    }
  }
}

.tab-container {
  padding: 10px;
}

.coach-phone {
  background-color: #eff8ec;
  color: #60c041;
  border-radius: 16px;
  padding: 6px;
  font-size: 12px;
  line-height: 1;
}

.coach-tip {
  color: #9e9e9e;
  font-size: 12px;
}

.sex-man {
  color: #3f89f4;
  background-color: #ebf3fd;
  width: 45px;
  height: 24px;
  border-radius: 6px;
  font-size: 12px;
  margin-right: 6px;

  .icon {
    width: 14px;
    height: 14px;
    margin-right: 4rpx;
  }
}

.sex-woman {
  color: #f284a3;
  background-color: #fef3f6;
  width: 45px;
  height: 24px;
  border-radius: 6px;
  font-size: 12px;
  margin-right: 6px;

  .icon {
    width: 14px;
    height: 14px;
    margin-right: 4px;
  }
}

.mgB-10 {
  margin-bottom: 5px;
}

.flex {
  display: flex;
}

.flex-between {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.flex-center {
  display: flex;
  justify-content: center;
  align-items: center;
}

.align-center {
  align-items: center;
}

.active-component {
  border: 1px solid var(--el-color-primary);
}
</style>
